﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dimas.Framework.Common.Base;
using Dimas.Framework.Entity;
using Dimas.Framework.Interface;
using Dimas.Framework.Interface.Repostory;
using StructureMap;

namespace Dimas.Portal.BL
{
    public class SampleProvider:BaseProvider
    {
        IADOSampleRepository _iADOSampleRepository;
        public SampleProvider(IADOSampleRepository iADOSampleRepository) 
        {
            _iADOSampleRepository = iADOSampleRepository;
        }


        public List<SampleInfo> GetWithInjection(SampleInfo sampleInfo)
        {
            List<SampleInfo> result = new List<SampleInfo>();

            var resultRepositoryA = _iADOSampleRepository.GetById(sampleInfo.Id);
            result.Add(resultRepositoryA);

            return result;
        }

        public List<SampleInfo> Get(SampleInfo sampleInfo)
        {
            logger.Debug("SampleProvider Get start");
            List<SampleInfo> result = new List<SampleInfo>();


            var repositoryA = ObjectFactory.GetInstance<IEarlyBindingSampleRepository>();
            var resultRepositoryA = repositoryA.GetById(sampleInfo.Id);
            result.Add(resultRepositoryA);

            var repositoryC = ObjectFactory.GetInstance<ILateBindingSampleRepository>();
            var resultRepositoryC = repositoryC.GetById(sampleInfo.Id);
            result.Add(resultRepositoryC);


            var cacheKey = "sampleCache" + sampleInfo.Id.ToString();
            
            if (!cache.Contains(cacheKey))
            {
                var obj = _iADOSampleRepository.GetById(sampleInfo.Id);
                cache.Add(cacheKey, obj, DateTimeOffset.MaxValue);
            }

            var resultRepositoryB = (SampleInfo)cache.Get(cacheKey);
            result.Add(resultRepositoryB);

            logger.Debug("SampleProvider Get end");
            return result;
        }

    }
}
